package com.rtx.edi.platform.structure.controller;

import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rtx.edi.model.api.dto.PlApiMappingDTO;
import com.rtx.edi.model.api.dto.PlApiMappingStructureDTO;
import com.rtx.edi.model.api.po.PlApiMappingStructure;
import com.rtx.edi.model.response.EdiResponse;
import com.rtx.edi.platform.structure.service.PlApiMappingStructureService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * <p>
 * 报文映射信息表 前端控制器
 * </p>
 *
 * @author cafebabe
 * @since 2023-03-10
 */
@Api(tags = {"报文映射管理"})
@RestController
@RequestMapping("/v1/edi/PlApiMappingStructure")
public class PlApiMappingStructureController {
	
	Logger log = LoggerFactory.getLogger(this.getClass());

	@Autowired
	private PlApiMappingStructureService plApiMappingStructureService;
	
	
	@GetMapping("download/structureExcel")
	public void downLoadExcel(HttpServletResponse response,HttpServletRequest request) throws IOException {
		PlApiMappingStructureDTO dto = new PlApiMappingStructureDTO();
		dto.setApiMappingId(Long.parseLong(request.getParameter("mappingid")));
		XSSFWorkbook workbook = plApiMappingStructureService.downLoadExcel(dto);
		response.addHeader("Content-Disposition", "attachment;filename=" + new String("mappingField.xlsx"));
		OutputStream out =new BufferedOutputStream( response.getOutputStream());
		response.setContentType("application/vnd.ms-template;charset=UTF-8");
		workbook.write(out);
		out.flush();
		out.close();
		
	}
	
	@PostMapping("upload")
	public EdiResponse uploadLoadExcel(MultipartFile multipartFile) {
		plApiMappingStructureService.uploadExcel(multipartFile);
		return EdiResponse.success();
	}
	
	/**
	 * 获取报文映射列表
	 * @return
	 */
	@ApiOperation(value = "查询_对接场景明细", notes = "查询_对接场景明细")
	@PostMapping("selectPlApiMappingStructure")
	public EdiResponse selectPlApiMappingStructureDetail(@RequestBody(required = false) PlApiMappingStructureDTO dto) {
		return EdiResponse.success().data("item",plApiMappingStructureService.selectPlApiMappingStructureDetail(dto));
	}
	
	/**
	 * 获取报文映射列表
	 * @return
	 */
	@ApiOperation(value = "查询_API信息", notes = "查询_API信息")
	@PostMapping("selectPlApiInfoById")
	public EdiResponse selectApi(@RequestBody(required = false) PlApiMappingDTO dto) {
		return  EdiResponse.success().data("item",plApiMappingStructureService.selectApi(dto));
	}
	
	/**
	 * 	查询对接场景下目标api结构
	 * @param dto
	 * @return
	 */
	@ApiOperation(value = "查询对接场景下目标api报文结构", notes = "查询对接场景下目标api报文结构")
	@PostMapping("queryStructureListByApiMappingId")
	public EdiResponse queryStructureByMappingId(@RequestBody(required = false) PlApiMappingStructureDTO dto) {
		return EdiResponse.success().data("item",plApiMappingStructureService.queryStructureByMappingId(dto));
	}
	
	/**
	 * 	查询对接场景下目标api结构
	 * @param dto
	 * @return
	 */
	@ApiOperation(value = "对接场景下目标api报文结构_分页查询", notes = "查询对接场景下目标api报文结构_分页查询")
	@PostMapping("queryStructurePageListByApiMappingId")
	public EdiResponse queryStructureByMappingIdPage(@RequestBody(required = false) PlApiMappingStructureDTO dto) {
		Page<PlApiMappingStructure> pageParam = new Page<>(dto.getPageNum(), dto.getPageSize());
		plApiMappingStructureService.selectStructurePage(pageParam,dto);
		return EdiResponse.success().data("page",pageParam);
	}
	
	/**
	 * 	新增目标api报文结构节点
	 * @return
	 */
	@ApiOperation(value = "新增目标api报文结构节点", notes = "新增目标api报文结构节点")
	@PostMapping("addStructure")
	public EdiResponse addStructure(@RequestBody PlApiMappingStructureDTO dto) {
		plApiMappingStructureService.addStructure(dto);
		return EdiResponse.success();
	}
	
	/**
	 * 	删除目标api报文结构节点以及下面子节点
	 * @return
	 */
	@ApiOperation(value = "删除目标api报文结构节点以及下面子节点", notes = "删除目标api报文结构节点以及下面子节点")
	@PostMapping("delStructureAndChild")
	public EdiResponse delStructureAndChild(@RequestBody PlApiMappingStructureDTO dto) {
		plApiMappingStructureService.delStructureNodeAndChild(dto);
		return EdiResponse.success();
	}
	
	/**
	 * 	更新api报文结构节点
	 * @return
	 */
	@ApiOperation(value = "更新api报文结构节点", notes = "更新api报文结构节点")
	@PostMapping("editStructure")
	public EdiResponse editStructure(@RequestBody PlApiMappingStructureDTO dto) {
		plApiMappingStructureService.editStructure(dto);
		return EdiResponse.success();
	}
	
	
	@ApiOperation(value = "获取sourcefield下拉框", notes = "获取sourcefield下拉框")
	@PostMapping("querySourceFieldselector")
	public EdiResponse querySourceField(@RequestBody(required = false) PlApiMappingDTO dto) {
		return EdiResponse.success().data("item",plApiMappingStructureService.querySourceField(dto));
	}
	
	@ApiOperation(value = "删除api_mapping下所有配置", notes = "删除api_mapping下所有配置")
	@PostMapping("deleteStructureByMappingid")
	public EdiResponse deleteApiMappingStructureByMappingid(@RequestBody(required = false) List<PlApiMappingDTO> dto) {
		plApiMappingStructureService.deleteApiMappingStructureByMappingid(dto.get(0));
		return  EdiResponse.success();
	}
	
}
